package com.coda.controller;



import com.coda.common.Result;
import com.coda.entity.LoginUser;
import com.coda.entity.Product;

import com.coda.entity.User;
import com.coda.service.LoginService;
import com.coda.service.TokenService;
import com.coda.service.UserService;
import com.google.code.kaptcha.Producer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.parser.Token;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/warehouse")
@Api(tags = "登录相关接口")
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;
    /**
     *生成图形验证码
     * @return
     */
    @GetMapping("/captcha/captchaImage")
    @ApiOperation("生成图形验证码")
    public void captcha(HttpServletResponse response){
        loginService.setCaptcha(response);
    }

    /**
     * 登录
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result login(@RequestBody LoginUser loginUser){
        String token = loginService.login(loginUser);
        return Result.ok("登录成功",token);
    }

    /**
     * 获取当前登录的用户信息
     */
    @GetMapping("/curr-user")
    @ApiOperation("获取登录用户信息")
    public Result getUserInfo(){
        User user = userService.getUserInfo();
        return Result.ok(user);
    }


    /**
     * 退出登录
     */
    // todo 退出登录 将token添加进黑名单带优化
    @DeleteMapping("/logout")
    @ApiOperation("退出登录")
    public Result logout(@RequestHeader("Token") String token){
        tokenService.addTokenBlackList(token);
        return Result.ok("退出登录");
    }
}
